package com.coder520.login.controller;

import com.coder520.common.untils.SecurityUtils;
import com.coder520.user.pojo.User;
import com.coder520.user.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Administrator on 2018/5/19.
 */
@Controller("loginController")
@RequestMapping("/Login")
public class LoginController {

    @Autowired
    private userService userService;

    @RequestMapping("/login")
    public String Login(){

        return "login";
    }

    //检查登入信息是否正确
    @RequestMapping("/check")
    @ResponseBody
    public String checkLogin(HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");

        //根据用户名去数据库查询
        User user = userService.findUserByusername(username);

        if(user!=null){
            //密码需加密后对比
            if(SecurityUtils.checkPassword(pwd,user.getPassword())){
                //将用户信息存入SESSION
                request.getSession().setAttribute("USER_SESSION",user);
                //返回正确信息给前端
                return "success";
            }else {
                return "fail";
            }
        }else {
            return "fail";
        }

    }


}
